package org.easy.amapgeo;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.easy.core.util.HttpUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * https://lbs.amap.com/api/webservice/guide/api/ipconfig
 */
public class AmapGeoClient {


    /**
     * 高德地图请求地址
     */
    private final static String REQUEST_URL_TEMPLATE = "https://restapi.amap.com/v3/geocode/regeo";


    /**
     * 实况天气
     *
     * @param key    请求服务权限标识 https://console.amap.com/dev/key/app 查看key
     * @param lng    经度在前
     * @param lat    纬度在后
     * @param radius 搜索半径  radius 取值范围：0~3000，默认值：1000。单位：米
     * @return
     */
    public GeoAddress getAddress(String key, String lng, String lat, Integer radius) {
        Map<String, String> params = new HashMap<>();
        params.put("key", key);
        //lng经度在前，lat纬度在后
        params.put("location", lng + "," + lat);
        params.put("extensions", "base");
        params.put("radius", String.valueOf(radius));

        String response = HttpUtil.sendGet(REQUEST_URL_TEMPLATE, null, params);
        JSONObject jsonObject = JSON.parseObject(response).getJSONObject("regeocode").getJSONObject("addressComponent");
        return JSON.parseObject(jsonObject.toJSONString(), GeoAddress.class);
    }


}